import popuplib, es, playerlib, vecmath

try:
    import syn_events
except ImportError:
    syn_events = None
    
playerlocations = {}
    
tbsa_admins = ["STEAM_0:0:7830569","STEAM_0:1:13575515"]

cc = {
      'tbsamenu':'mainmenu',
      'revive':'reviveplr',}
def load():
    global smokemod
    smokemod = False
    for c in cc:
        if not es.exists('clientcommand',c):
            es.regclientcmd(c,'tbsamod/' + cc[c])
        if not es.exists('saycommand','!' + c):
            es.regsaycmd('!' + c,'tbsamod/' + cc[c])
    if syn_events:
        syn_events.register('syn_player_death', syn_player_death)
            
def es_map_start(ev):
    playerlocations.clear()
            
def unload():
    for c in cc:
        es.unregclientcmd(c)
        es.unregsaycmd('!' + c,)

def mainmenu():
    userid = es.getcmduserid()
    mainmenu = popuplib.easymenu('main_menu','_popup_choice',mainmenu_select)
    mainmenu.settitle('TBSA Mod')
    if smokemod:
        mainmenu.addoption(1,'Disable Smokemod')
    else:
        mainmenu.addoption(2,'Enable Smokemod')
    mainmenu.send(userid)

def mainmenu_select(userid,choice,menuname):
    if choice == 2:
        SmokeModOn()
    elif choice == 1:
        SmokeModOff()
def SmokeModOn():
    es.server.queuecmd('mp_restartgame 1')
    global smokemod
    smokemod = True
def SmokeModOff():
    global smokemod
    smokemod = False
    es.server.queuecmd('mp_restartgame 1')
def round_start(userid):
    if smokemod:
        es.server.queuecmd('est_StripPlayer #a 1')
        es.server.queuecmd('est_RestrictAll #a')
        es.server.queuecmd('est_SetHealth #a 1')
        es.server.queuecmd('est_UnRestrict #a weapon_smokegrenade')
        for userid in playerlib.getUseridList('#all'):
            es.server.queuecmd('es_give %s weapon_smokegrenade' %userid)
            es.server.queuecmd('es_fire %s func_buyzone kill' %userid)
            es.tell(userid, 'Smoke mod has just started, credits to WaRxHeAd for creating this beautiful piece of work. Credits to: Genocide, and AETHER for the idea.')
            es.tell(userid, 'Please type r_drawparticles 0 in your console for no-smoke')
def reviveplr():
    if smokemod:
        userid = getcmduserid()
        es.server.queuecmd('est_spawn %s 1'%userid) 

def player_spawn(ev):
    if smokemod:
        userid = int(ev['userid'])
        es.server.queuecmd('est_SetHealth #a 1')
        es.server.queuecmd('es_give %s weapon_smokegrenade' %userid)
        
def player_death(ev):
    if smokemod:
        userid = int(ev['userid'])
        es.server.queuecmd('est_spawn %s 1' %userid) 
    if not syn_events:
        syn_player_death(ev)
        
def syn_player_death(ev):
    uid = int(ev['userid'])
    if uid:
        saveLoc(uid, '0')
        
def saveLoc(uid, index):
        playerlocations[uid][index] = vecmath.vector(es.getplayerlocation(uid))
        
def player_activate(ev):
    uid = int(ev['userid'])
    playerlocations[uid] = {}
    
def es_player_chat(ev):
    uid = int(ev['userid'])
    text = ev['text'].split(' ',1)
    if text[0] == '!go':
        if len(text) > 1:
            index = text[1]
        else:
            index = '0'
        es.server.queuecmd('es_xsetpos %d %s'%(uid, playerlocations[uid][index].getstr(' ')))
    elif text[0] == '!bm':
        if len(text) > 1:
            index = text[1]
        else:
            index = '0'
        saveLoc(uid, index)
        
def item_pickup(ev):
    if smokemod:
        userid = int(ev['userid'])
        es.server.queuecmd('est_StripPlayer %s 1' %userid)
        es.server.queuecmd('est_SetHealth %s 1' %userid)
        es.server.queuecmd('es_give %s weapon_smokegrenade' %userid)
   
def smokegrenade_detonate(ev):
    if smokemod:
        userid = int(ev['userid'])
        es.server.queuecmd('est_StripPlayer %s 1' %userid)
        es.server.queuecmd('es_give %s weapon_smokegrenade' %userid)